﻿using Synergy.dbmodel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Synergy
{
    public partial class Form改BOM : Form
    {
        private BindingSource bs = new BindingSource();
        public Form改BOM()
        {
            InitializeComponent();
        }

        private void button导入_Click(object sender, EventArgs e)
        {
            //选择excel文件并导入
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm";
            openFileDialog.Title = "选择Excel文件";
            openFileDialog.CheckFileExists = true;
            openFileDialog.CheckPathExists = true;
            openFileDialog.Multiselect = false;
            //通过miniexcel读取为 Z_改BOM对下
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                dataGridView1.DataSource = null;
                string filePath = openFileDialog.FileName;
                //读取excel文件
                List<Z_改BOM> dt = MiniExcelLibs.MiniExcel.Query<Z_改BOM>(filePath).ToList();
                //如果有 修改子件编码 字段是空白的就提示
                if (dt.Any(x => string.IsNullOrEmpty(x.修改子件编码)))
                {
                    MessageBox.Show("有[修改子件编码]是空白的，所有都不能为空，请修改");
                    return;
                }
                else
                {
                    //绑定到datagridview
                    bs.DataSource = dt;
                    dataGridView1.DataSource = bs;
                    bs.ResetBindings(true);
                }
            }
        }

        private async void button更新U8_Click(object sender, EventArgs e)
        {
            Guid guid = Guid.NewGuid();
            //如果没有数据源，就是提示不能为空
            if (dataGridView1.DataSource == null || bs.DataSource == null || bs.Count <= 0)
            {
                MessageBox.Show("没有要改的数据源，请导入数据");
                return;
            }
            else
            {
                foreach (Z_改BOM item in bs)
                {
                    
                    string sql = "";
                    if (item.修改子件用量.GetValueOrDefault() > 0)
                    {
                        sql = $@"UPDATE bom_opcomponent SET ComponentId = (    SELECT TOP 1 PartId     FROM bas_part     WHERE InvCode = '{item.修改子件编码}'),BaseQtyN = {item.修改子件用量}	where  OpComponentId =	{item.OpComponentId}	and ComponentId =	{item.componentId}";

                    }
                    else
                    {
                        sql = $@"UPDATE bom_opcomponent SET ComponentId = (    SELECT TOP 1 PartId     FROM bas_part     WHERE InvCode = '{item.修改子件编码}')	where  OpComponentId =	{item.OpComponentId}	and ComponentId =	{item.componentId}";
                    }

                    int r = await SqlSugarHelp.scope.Ado.ExecuteCommandAsync(sql);
                    item.guid = guid;
                    item.修改结果 = r;
                    item.修改时间 = DateTime.Now;
                    item.sql = sql;
                    //找到对应的行
                    var row = bs.List.OfType<Z_改BOM>().FirstOrDefault(x => x.OpComponentId == item.OpComponentId);
                    if (row != null)
                    {
                        //更新行
                        row.修改结果 = item.修改结果;
                        row.修改时间 = item.修改时间;
                    }
                    //刷新界面
                    bs.ResetBindings(true);
                    await SqlSugarHelp.scope.Insertable(item).ExecuteCommandAsync();
                }
                查询(guid);
                MessageBox.Show("更新完成,请到U8查看");
            }
        }

        private void 查询(Guid guid)
        {
            bs.DataSource = SqlSugarHelp.scope.Queryable<Z_改BOM>().Where(it => it.guid == guid).ToList();
            dataGridView1.DataSource = bs;
            bs.ResetBindings(true);
        }

        private void button导出模板_Click(object sender, EventArgs e)
        {
            string sql = "select [版本号],[版本说明],[母件编码],[母件名称],[母件规格] ,[子件编码] ,[子件名称],[子件规格],[OpComponentId],[componentId],[用量],[修改子件编码],[修改子件用量] from Z_改BOM where 1=2";
            DataTable dt = SqlSugarHelp.scope.Ado.GetDataTable(sql);
            //选择导出路径
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm";
            saveFileDialog.Title = "导出Excel文件";
            saveFileDialog.CheckFileExists = false;
            saveFileDialog.CheckPathExists = true;
            saveFileDialog.CreatePrompt = true;
            saveFileDialog.OverwritePrompt = true;
            saveFileDialog.FileName = "改BOM模板" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx";
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                string filePath = saveFileDialog.FileName;
                //导出excel文件
                MiniExcelLibs.MiniExcel.SaveAs(filePath, dt);
                MessageBox.Show("导出成功");
            }

        }
    }
}
